//
//  SinaAccountViewModel.m
//  oc微博
//
//  Created by 安陆与 on 16/1/19.
//  Copyright © 2016年 安陆与. All rights reserved.
//

#import "SinaAccountViewModel.h"
#import "SinaAccountModel.h"
#import "SinaOauthViewController.h"
#import "AFNetworking.h"
#import "SVProgressHUD.h"
#import "AFNetworkActivityIndicatorManager.h"

#import "SinaAccountModel.h"

#define app_Key @"4094036286"
#define app_Secret @"ba6e1d99887953f43ef9e698755c574f"
#define redirect_Url @"http://www.baidu.com"


@implementation SinaAccountViewModel

//static SinaAccountViewModel* _instance;
//+(instancetype)shareAccount{
//    static dispatch_once_t oneToken;
//    dispatch_once(&oneToken, ^{
//        
//        _instance = [[self alloc] init];
//    });
//    
//    return _instance;
//}

-(BOOL)isLogin{

    if (_model.access_token) {
        return YES;
    }
    else{
        return NO;
    }

}

-(NSString *)profile_image_url{

    return _model.profile_image_url;
}

-(NSString *)access_token{

    return _model.access_token;
}

-(instancetype)init{

    self = [super init];
    
    _model = [SinaAccountModel readInfo];
    
    return self;

}

//获取token
-(void)getAccessTokenWithCode:(NSString *)code{
    
    //创建manger
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    //创建url
    NSString *url = @"https://api.weibo.com/oauth2/access_token";
    
    //增加解析类型
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/plain", nil];
    
    //拼接字符串
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    params[@"client_id"] = app_Key;
    params[@"client_secret"] = app_Secret;
    params[@"grant_type"] = @"authorization_code";
    params[@"code"] = code;
    params[@"redirect_uri"] = redirect_Url;
    
    //发送数据请求
    [manager POST:url parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        SinaAccountModel *account = [SinaAccountModel accountWithDict:responseObject];
        NSLog(@"字典转模型输出token%@",account.access_token);
        
        //获取个人信息的调用
        [self getPersonInfo:account];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        
    }];
}

//获取token之后,获取个人信息
-(void)getPersonInfo:(SinaAccountModel *)account{
    
    //添加一个判断是否有token和uid
    if (account.uid == nil || account.access_token == nil) {
        NSLog(@"token和uid都不能为空!");
    }
    
    //获取用户星系
    //1.获取manager
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/plain", nil];
    
    //创建url
    //    NSURL *url = [NSURL URLWithString:@"https://api.weibo.com/2/users/show.json"];
    NSString *url = @"https://api.weibo.com/2/users/show.json";
    //拼接参数
    NSMutableDictionary *parames = [[NSMutableDictionary alloc]init];
    
    parames[@"access_token"] = account.access_token;
    parames[@"uid"] = account.uid;
    
    //2.发送请求
    
    [manager GET:url parameters:parames success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //这里给用户的头像和昵称属性赋值
        account.screen_name = responseObject[@"screen_name"];
        account.profile_image_url = responseObject[@"profile_image_url"];
        
        //保存信息
        [account saveInfo];
//                SinaAccountModel *model = [SinaAccountModel readInfo];
//                NSLog(@"%@,%@",model.screen_name,model.profile_image_url);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
    
}


@end
